﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.WorkFlow_Processs.Authorization;
using LeadThing.AbpZeroTemplate.WorkFlow_Processs.Dtos;
using LeadThing.AbpZeroTemplate.Dto;
using LeadThing.AbpZeroTemplate.WorkFlows;

namespace LeadThing.AbpZeroTemplate.WorkFlow_Processs
{
    /// <summary>
    /// 流程节点配置表服务实现
    /// </summary>
    //[AbpAuthorize(WorkFlow_ProcessAppPermissions.WorkFlow_Process)]


    public class WorkFlow_ProcessAppService : AbpZeroTemplateAppServiceBase, IWorkFlow_ProcessAppService
    {
        private readonly IRepository<WorkFlow_Process, long> _workFlow_ProcessRepository;
        private readonly IWorkFlow_ProcessListExcelExporter _workFlow_ProcessListExcelExporter;
        private readonly IRepository<WorkFlow, long> _workFlowRepository;


        private readonly WorkFlow_ProcessManage _workFlow_ProcessManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public WorkFlow_ProcessAppService(IRepository<WorkFlow_Process, long> workFlow_ProcessRepository,
WorkFlow_ProcessManage workFlow_ProcessManage
      , IWorkFlow_ProcessListExcelExporter workFlow_ProcessListExcelExporter, IRepository<WorkFlow, long> workFlowRepository
  )
        {
            _workFlow_ProcessRepository = workFlow_ProcessRepository;
            _workFlow_ProcessManage = workFlow_ProcessManage;
            _workFlow_ProcessListExcelExporter = workFlow_ProcessListExcelExporter;
            _workFlowRepository = workFlowRepository;
        }


        #region 实体的自定义扩展方法
        private IQueryable<WorkFlow_Process> _workFlow_ProcessRepositoryAsNoTrack => _workFlow_ProcessRepository.GetAll().AsNoTracking();

        #endregion


        #region 流程节点配置表管理

        /// <summary>
        /// 根据查询条件获取流程节点配置表分页列表
        /// </summary>
        public async Task<PagedResultDto<WorkFlow_ProcessListDto>> GetPagedWorkFlow_ProcesssAsync(GetWorkFlow_ProcessInput input)
        {

            var query = _workFlow_ProcessRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var workFlow_ProcessCount = await query.CountAsync();

            var workFlow_Processs = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var workFlowList = _workFlowRepository.GetAll();

            return new PagedResultDto<WorkFlow_ProcessListDto>(workFlow_ProcessCount,
            workFlow_Processs.Select(item =>
            {
                var dto = item.MapTo<WorkFlow_ProcessListDto>();

                var wfModel = workFlowList.Where(p => p.Id == dto.FlowID).FirstOrDefault();
                if (wfModel != null)
                    dto.FlowName = wfModel.FlowName;
                return dto;
            }).ToList());
        }

        /// <summary>
        /// 通过Id获取流程节点配置表信息进行编辑或修改 
        /// </summary>
        public async Task<GetWorkFlow_ProcessForEditOutput> GetWorkFlow_ProcessForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetWorkFlow_ProcessForEditOutput();

            WorkFlow_ProcessEditDto workFlow_ProcessEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _workFlow_ProcessRepository.GetAsync(input.Id.Value);
                workFlow_ProcessEditDto = entity.MapTo<WorkFlow_ProcessEditDto>();
            }
            else
            {
                workFlow_ProcessEditDto = new WorkFlow_ProcessEditDto();
            }

            output.WorkFlow_Process = workFlow_ProcessEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取流程节点配置表ListDto信息
        /// </summary>
        public async Task<WorkFlow_ProcessListDto> GetWorkFlow_ProcessByIdAsync(EntityDto<long> input)
        {
            var entity = await _workFlow_ProcessRepository.GetAsync(input.Id);

            return entity.MapTo<WorkFlow_ProcessListDto>();
        }







        /// <summary>
        /// 新增或更改流程节点配置表
        /// </summary>
        public async Task CreateOrUpdateWorkFlow_ProcessAsync(CreateOrUpdateWorkFlow_ProcessInput input)
        {
            if (input.WorkFlow_ProcessEditDto.Id.HasValue)
            {
                await UpdateWorkFlow_ProcessAsync(input.WorkFlow_ProcessEditDto);
            }
            else
            {
                await CreateWorkFlow_ProcessAsync(input.WorkFlow_ProcessEditDto);
            }
        }

        /// <summary>
        /// 新增流程节点配置表
        /// </summary>
        //[AbpAuthorize(WorkFlow_ProcessAppPermissions.WorkFlow_Process_CreateWorkFlow_Process)]
        public virtual async Task<WorkFlow_ProcessEditDto> CreateWorkFlow_ProcessAsync(WorkFlow_ProcessEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<WorkFlow_Process>();

            entity = await _workFlow_ProcessRepository.InsertAsync(entity);
            return entity.MapTo<WorkFlow_ProcessEditDto>();
        }

        /// <summary>
        /// 编辑流程节点配置表
        /// </summary>
        //[AbpAuthorize(WorkFlow_ProcessAppPermissions.WorkFlow_Process_EditWorkFlow_Process)]
        public virtual async Task UpdateWorkFlow_ProcessAsync(WorkFlow_ProcessEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _workFlow_ProcessRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _workFlow_ProcessRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除流程节点配置表
        /// </summary>
        //[AbpAuthorize(WorkFlow_ProcessAppPermissions.WorkFlow_Process_DeleteWorkFlow_Process)]
        public async Task DeleteWorkFlow_ProcessAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _workFlow_ProcessRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除流程节点配置表
        /// </summary>
        //[AbpAuthorize(WorkFlow_ProcessAppPermissions.WorkFlow_Process_DeleteWorkFlow_Process)]
        public async Task BatchDeleteWorkFlow_ProcessAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _workFlow_ProcessRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 流程节点配置表的Excel导出功能


        public async Task<FileDto> GetWorkFlow_ProcessToExcel()
        {
            var entities = await _workFlow_ProcessRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<WorkFlow_ProcessListDto>>();

            var fileDto = _workFlow_ProcessListExcelExporter.ExportWorkFlow_ProcessToFile(dtos);



            return fileDto;
        }


        #endregion

    }
}
